package com.cxg.fairy.ec.main.search;

import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.ec.detail.GoodsDetailDelegate;
import com.cxg.fairy.ui.recycler.MultipleFields;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;
import com.cxg.fairy.ui.recycler.MultipleRecyclerAdapter;
import com.cxg.fairy.ui.recycler.MultipleViewHolder;
import com.diabin.latte.ec.R;

import java.util.List;

/**
 * Created by CFairy
 */

public final class SearchListAdapter extends MultipleRecyclerAdapter {

    private LatteDelegate DELEGATE;

    private static final RequestOptions OPTIONS = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .centerCrop()
            .dontAnimate();

    SearchListAdapter(List<MultipleItemEntity> data) {
        super(data);

        //添加购物测item布局
        addItemType(SearchListItemType.SEARCH_LIST_MIGHT_LIKE_GOODS,R.layout.item_search_list_goods);
    }

    @Override
    protected void convert(final MultipleViewHolder holder, final MultipleItemEntity entity) {
        super.convert(holder, entity);

        //先取出所有值
        final String thumb = entity.getField(MultipleFields.IMAGE_URL);
        final String price = entity.getField(SearchListItemFields.PRICE);
        final String unit = entity.getField(SearchListItemFields.UNIT);
        final String goodsName = entity.getField(SearchListItemFields.GOODS_NAME);


        //所有控件
        final AppCompatImageView ivSuggestImg = holder.getView(R.id.iv_cart_suggest_item_img);
        final AppCompatTextView tvSuggestName = holder.getView(R.id.tv_cart_suggest_item_name);
        final AppCompatTextView tvSuggestUnit = holder.getView(R.id.tv_cart_suggest_item_unit);
        final AppCompatTextView tvSuggestPrice = holder.getView(R.id.tv_cart_suggest_item_price);

        switch (holder.getItemViewType()) {

            case SearchListItemType.SEARCH_LIST_MIGHT_LIKE_GOODS:
                tvSuggestUnit.setText(unit);
                tvSuggestPrice.setText(price);
                tvSuggestName.setText(goodsName);
                Glide.with(mContext)
                        .load(thumb)
                        .apply(OPTIONS)
                        .into(ivSuggestImg);

                break;

            default:
                break;
        }
    }

    @Override
    public void onItemClick(int position) {
        super.onItemClick(position);

    }

    @Override
    public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
        return getData().get(position).getField(SearchListItemFields.SPAN_SIZE);
    }

    @Override
    public int getItemCount() {
        return super.getItemCount();
    }
}
